$OpenBSD: patch-icb_unix_c,v 1.2 2005/12/31 02:09:46 fgsch Exp $
--- icb/unix.c.orig	Fri Feb 24 21:20:31 1995
+++ icb/unix.c	Sat Dec 31 01:54:33 2005
@@ -9,12 +9,36 @@
 #include "externs.h"
 #include <pwd.h>
 #include <sys/dir.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #undef stty
 #undef gtty
 
-#ifndef SYSV
+#ifdef SYSV
 
+#define USE_TERMIO
+#include <termio.h>
+#define TTYSTRUCT termio
+#define stty(fd,buf) ioctl((fd),TCSETA,(buf))
+#define gtty(fd,buf) ioctl((fd),TCGETA,(buf))
+
+#else /* SYSV */
+
+# ifdef BSD4_4
+# define USE_TERMIO
+# include <termios.h>
+# define TTYSTRUCT termios
+# define stty(fd,buf) ioctl((fd),TIOCSETA,(buf))
+# define gtty(fd,buf) ioctl((fd),TIOCGETA,(buf))
+
+# endif /* BSD4_4 */
+
+#endif /* SYSV */
+
+#ifndef USE_TERMIO /* neither case above (SYSV or BSD4_4) */
+
 #ifdef linux
 #include <bsd/sgtty.h>
 #else
@@ -22,18 +46,15 @@
 #endif
 
 #define TTYSTRUCT sgttyb
-#define stty(fd,buf) ioctl((fd),TIOCSETN,(buf))
+#define stty(fd,buf) ioctl((fd),TIOCSETP,(buf))
 #define gtty(fd,buf) ioctl((fd),TIOCGETP,(buf))
-#else /* SYSV */
-#include <termio.h>
-#define TTYSTRUCT termio
-#define stty(fd,buf) ioctl((fd),TCSETA,(buf))
-#define gtty(fd,buf) ioctl((fd),TCGETA,(buf))
-#endif /* SYSV */
+#endif
 
 char *getlogin();
 
 struct TTYSTRUCT origtty;	/* holds the user's original term settings */
+struct stat origtty_stats;	/* holds termainal stats */
+char *mytty = NULL;		/* holds terminal name */
 
 int badttyinfo = 0;	/* used when running under some weird modes */
 
@@ -79,13 +100,13 @@ FILE *fp;
 pushback(c)
 char c;
 {
-#ifndef SYSV
+#ifndef USE_TERMIO
 	if (ioctl(0, TIOCSTI, &c) < 0)
 		perror("TIOCSTI ioctl failed");
-#else /* SYSV */
+#else /* USE_TERMIO */
 	if (ungetc(c,stdin) == EOF)
 		perror("ungetc() failed");
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 }
 
 
@@ -94,25 +115,30 @@ char c;
 
 getterm()
 {
-#ifndef SYSV
+	/* get terminal stats */
+	if ((mytty = ttyname(STDERR_FILENO))) {
+		if (stat(mytty, &origtty_stats)) mytty = NULL;
+	}
+
+#ifndef USE_TERMIO
 	struct ltchars lt;
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 	/* get tty settings */
 	if (gtty(0,&origtty) < 0) {
 		badttyinfo++;
 		ttyinfo.erase = '\b';	/* ^H */
 		ttyinfo.kill = '\025';	/* ^U */
 	} else {
-#ifndef SYSV
+#ifndef USE_TERMIO
 		ttyinfo.erase = origtty.sg_erase;
 		ttyinfo.kill = origtty.sg_kill;
-#else /* SYSV */
+#else /* USE_TERMIO */
 		ttyinfo.erase = origtty.c_cc[VERASE];
 		ttyinfo.kill = origtty.c_cc[VKILL];
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 	}
 
-#ifndef SYSV
+#ifndef USE_TERMIO
 	/* get local special chars */
  	if (ioctl(0, TIOCGLTC, &lt) < 0) {
 		ttyinfo.redraw = '\022'; /* ^R */
@@ -121,10 +147,10 @@ getterm()
 		ttyinfo.redraw = lt.t_rprntc;
 		ttyinfo.werase = lt.t_werasc;
 	}
-#else /* SYSV */
+#else /* USE_TERMIO */
 	ttyinfo.redraw = '\022'; /* ^R */
 	ttyinfo.werase = '\027'; /* ^W */
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 
 	/* get the current window size */
 	getwinsize();
@@ -138,21 +164,24 @@ icbterm()
 {
 	struct TTYSTRUCT tty;
 
+	/* disable biff/mesg */
+	if (mytty)
+		chmod(mytty, origtty_stats.st_mode & ~(S_IXUSR | S_IWGRP));
+
 	if (badttyinfo)
 		return;
 
-
 	bcopy((char *)&origtty, (char *)&tty, (unsigned)sizeof(struct TTYSTRUCT));
 
 	/* turn on cbreak - turn off echo */
-#ifndef SYSV
+#ifndef USE_TERMIO
 	tty.sg_flags |= CBREAK;
 	tty.sg_flags &= ~ECHO;
-#else /* SYSV */
+#else /* USE_TERMIO */
 	tty.c_lflag  &= ~ICANON;
 	tty.c_cc[VEOF] = 1;
 	tty.c_lflag &= ~ECHO;
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 
 	echomode = 0;
 	/* set the new flags */
@@ -167,6 +196,10 @@ icbterm()
 
 restoreterm()
 {
+	/* restore terminal stats */
+	if (mytty)
+		chmod(mytty, origtty_stats.st_mode);
+
 	if (badttyinfo)
 		return;
 
@@ -258,11 +291,11 @@ echo()
 	}
 
 	/* turn on echo */
-#ifndef SYSV
+#ifndef USE_TERMIO
 	tty.sg_flags |= ECHO;
-#else /* SYSV */
+#else /* USE_TERMIO */
 	tty.c_lflag  |= ECHO;
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 
 	echomode = 1;
 
@@ -287,11 +320,11 @@ noecho()
 	}
 
 	/* turn off echo */
-#ifndef SYSV
+#ifndef USE_TERMIO
 	tty.sg_flags &= ~ECHO;
-#else /* SYSV */
+#else /* USE_TERMIO */
 	tty.c_lflag  &= ~ECHO;
-#endif /* SYSV */
+#endif /* USE_TERMIO */
 
 	echomode = 0;
 
